package com.yxplearn.mybatisplus.mybatisplus_simple;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yxplearn.mybatisplus.mybatisplus_simple.mapper.UserMapper;
import com.yxplearn.mybatisplus.mybatisplus_simple.pojo.User;
import com.yxplearn.mybatisplus.mybatisplus_simple.service.UserService;
import com.yxplearn.mybatisplus.util.RandomUtils;
import org.apache.logging.log4j.core.config.Configurator;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.*;
import java.util.logging.Handler;
import java.util.stream.Collectors;

public class ApplicationPlus {

    private UserMapper userMapper;
    private UserService userService;

    @Before
    public void initSpring() {
        System.setProperty("appName", "mybatisplus");
        System.setProperty("logInfo", "xxpp");
        Configurator.initialize("log4j2", "log4j2-sit.xml");
        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:mybatisplus_simple/application-context.xml");
        userMapper = context.getBean(UserMapper.class);
        userService = context.getBean(UserService.class);
    }

    @Test
    public void getAllUser() {
        System.out.println(userMapper.getAllUser());
    }

    @Test
    public void selectMaps() {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().select(User::getName, User::getEmail);
        System.out.println(userMapper.selectMaps(queryWrapper));
    }

    @Test
    public void selectByMap() {
        Map<String, Object> map = new HashMap<>();
        map.put("name", "IjRWLnznz");
        System.out.println(userMapper.selectByMap(map));
    }

    @Test
    public void selectObjs() {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.inSql("id", "select id from user where id <= 3");
        //selectObjs的使用场景：只返回一列
        List<Object> objects = userMapper.selectObjs(queryWrapper);
        objects.forEach(System.out::println);
    }

    @Test
    public void updateWrapper() {
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().set(User::getAge, 10084).eq(User::getId, 1528199321042792449l);
        userMapper.update(null, updateWrapper);
    }

    @Test
    public void updateConditionWrapper() {
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().set(User::getAge, 234123413).eq(true, User::getAge, 10084);
        userMapper.update(null, updateWrapper);
    }


    @Test
    public void userServiceCount() {
        System.out.println(userService.count());
    }

    @Test
    public void saveBatch() {
        List<User> userList = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            User user = new User();
            user.setAge(RandomUtils.getIntegerMoreThanZero());
            user.setEmail(RandomUtils.getIntegerMoreThanZero() + "@qq.email");
            user.setName(RandomUtils.getStringWithCharacter(9));
            userList.add(user);
        }
        userService.saveBatch(userList);
    }

    @Test
    public void deleteBatchUser() {
        List<Long> collect = userMapper.getAllUser().stream().map(User::getId).collect(Collectors.toList());
        userService.removeByIds(collect);
    }

    @Test
    public void deleteUserByWrapper() {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(User::getId, 1528199321042792449l);
        userService.remove(queryWrapper);
    }

    @Test
    public void selectPage() {
        Page<User> page = new Page<>(1, 5);
        userMapper.selectPage(page, null);
        List<User> userList = page.getRecords();
        userList.forEach(System.out::println);
        pageToString(page);
    }

    /**
     * 自定义分页
     */
    @Test
    public void selectMyPage() {
        Page<User> page = new Page<>(2, 5);
        userMapper.selectPageByAge(page, 1711272941);
        List<User> userList = page.getRecords();
        userList.forEach(System.out::println);
        pageToString(page);
    }

    private void pageToString(Page<User> page) {
        System.out.println("当前页：" + page.getCurrent());
        System.out.println("每页显示的条数：" + page.getSize());
        System.out.println("总记录数：" + page.getTotal());
        System.out.println("总页数：" + page.getPages());
        System.out.println("是否有上一页：" + page.hasPrevious());
        System.out.println("是否有下一页：" + page.hasNext());
    }

    @Test
    public void baseTypeHandlerInsert() {
        User user = new User();
        user.setId(491416674l);
        Map<String, Integer> hashMap = new HashMap();
        hashMap.put("1", 2);
        hashMap.put("3", 3);
        hashMap.put("4", 4);
        user.setAttr(hashMap);
        user.setAge(RandomUtils.getIntegerMoreThanZero());
        Set<Integer> integerList = new HashSet<>();
        integerList.add(1);
        integerList.add(2);
        integerList.add(3);
        user.setSetTest(integerList);
        userMapper.updateById(user);
    }

    @Test
    public void baseTypeHandlerSelect() {
        User user = userMapper.selectById(491416674);
        System.out.println(user);
    }
}
